from selenium.webdriver.common.by import By

from cloud_po.pages.base_page import BasePage
from cloud_po.utils.log_utils import logger


class OrderPage(BasePage):

    __ADDRESSEE = (By.XPATH,"//*[@class='goods-body']//span[contains(text(), '收货信息')]")
    __PHONE = (By.XPATH,"//div[@class='goods-body']//span[contains(text(), '收货信息')]/following-sibling::span[1]")
    __INPUT_ONAME = (By.XPATH,"//*[text()='收件人']/..//input")
    __INPUT_OPHONE = (By.XPATH,"//*[text()='手机号']/..//input")
    __SEARCH_BTN =  (By.XPATH,"//*[@class='el-icon-search']/..")
    __INPUT_MONEY = (By.XPATH,"[placeholder='请输入订单金额']")
    __CONFIRM_BTN = (By.XPATH,"//*[text()='确认']")

    def get_order_list(self):
        logger.info("获取待发货页面订单")
        addr_elements = self.do_finds(self.__ADDRESSEE)
        name_list = [element.text.split('：')[1].strip() for element in addr_elements]

        phone_elements = self.do_finds(self.__PHONE)
        phone_list = [element.text for element in phone_elements]

        res_dict= {"name": name_list, "phone": phone_list}
        return res_dict


    def search_by_name(self, name):
        logger.info("收件人姓名输入框输入内容，点击搜索")
        # todo 输入框不点击直接能不能输入进去
        self.do_find(self.__INPUT_ONAME).click()
        self.do_send_keys(name,self.__INPUT_ONAME)
        self.do_find(self.__SEARCH_BTN).click()
        return self.get_order_list()

    def search_by_phone(self, phone):
        logger.info("收件人手机号输入框输入内容，点击搜索")
        self.do_find(self.__INPUT_OPHONE).click()
        self.do_send_keys(phone,self.__INPUT_OPHONE)
        self.do_find(self.__SEARCH_BTN).click()
        return self.get_order_list()

    def modify_order(self,order_id,money):
        logger.info("根据订单号修改订单金额")
        # 两个单引号 todo
        self.do_find(By.XPATH,f"//*[text()='订单编号：'{order_id}'']/../../../../..//*[text()='修改订单金额']").click()
        self.do_find(self.__SEARCH_BTN).click()
        self.do_send_keys(money,self.__INPUT_MONEY)
        self.do_find(self.__CONFIRM_BTN).click()
        return OrderPage(self.driver)

    def get_money_by_id(self,order_id):
        logger.info("根据订单号获取订单金额")
        ele = self.do_find(By.XPATH,f""""//*[text()="订单编号：'{order_id}'"]/../..//span[text()="订单金额:"]/following-sibling::span[1]""")
        updated_money = ele.text
        return updated_money







